
package com.zj.component;
import com.zj.config.SmsConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.concurrent.TimeUnit;

/**
 * @author zj
 * @version 1.0.0
 * @ClassName SmsComponent
 * @Description 短信验证码
 * @createTime 2024年11月19日 21:24
 */
@Component
@Slf4j
public class SmsComponent {

    private static final String URL_TEMPLATE = "https://zwp.market.alicloudapi.com/sms/sendv2?mobile=%s&content=%s";

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private SmsConfig smsConfig;

    /**
     * send message success
     * @param mobile
     * @param content
     */
    //用我定义的线程池
    @Async("threadPoolTaskExecutor")
    public void send(String mobile,String content){
        String url = String.format(URL_TEMPLATE,mobile,content);
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Authorization","APPCODE "+smsConfig.getAppCode());
        HttpEntity httpEntity = new HttpEntity(httpHeaders);
        ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, httpEntity, String.class);
        log.info("url=>{},body=>{}",url,response.getBody());
        if (response.getStatusCode().is2xxSuccessful()){
            log.info("send success");
        }
    }

    @Async("threadPoolTaskExecutor")
    public void testSend(){
        try {
            TimeUnit.MILLISECONDS.sleep( 2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
